package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyDebugBean;
import flc.ast.databinding.ActivityAddDebugBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.Str2NumUtil;

public class AddDebugActivity extends BaseAc<ActivityAddDebugBinding> {

    public static int type;
    public static int selPosition;
    public static boolean isEdit;
    private List<MyDebugBean> listSp = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_add_debug;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivAddDebugBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivNameClear.setOnClickListener(this);
        mDataBinding.ivHostClear.setOnClickListener(this);
        mDataBinding.ivPortClear.setOnClickListener(this);
        mDataBinding.tvAddDebugSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivNameClear://名称清空
                mDataBinding.etName.setText("");
                break;
            case R.id.ivHostClear://主机清空
                mDataBinding.etHost.setText("");
                break;
            case R.id.ivPortClear://端口清空
                mDataBinding.etPort.setText("");
                break;
            case R.id.tvAddDebugSave://保存
                String name = mDataBinding.etName.getText().toString().trim();
                String host = mDataBinding.etHost.getText().toString().trim();
                String port = mDataBinding.etPort.getText().toString().trim();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(host) || TextUtils.isEmpty(port))
                    return;
                if (isEdit) {
                    listSp.get(selPosition).setName(name);
                    listSp.get(selPosition).setHost(host);
                    listSp.get(selPosition).setPort(port);
                    if (type == 0) {
                        AppSpUtils.saveTcpServerList(listSp);
                    } else if (type == 1) {
                        AppSpUtils.saveTcpClientList(listSp);
                    } else if (type == 2) {
                        AppSpUtils.saveUdpServerList(listSp);
                    } else if (type == 3) {
                        AppSpUtils.saveUdpClientList(listSp);
                    }
                } else if (type == 0) {
                    List<MyDebugBean> listSp = AppSpUtils.getTcpServerList();
                    String type = getString(R.string.tcp_server);
                    if (listSp == null) {
                        listSp = new ArrayList<>();
                    }
                    listSp.add(new MyDebugBean(name, host, port, type));
                    AppSpUtils.saveTcpServerList(listSp);
                } else if (type == 1) {
                    List<MyDebugBean> listSp = AppSpUtils.getTcpClientList();
                    String type = getString(R.string.tcp_client);
                    if (listSp == null) {
                        listSp = new ArrayList<>();
                    }
                    listSp.add(new MyDebugBean(name, host, port, type));
                    AppSpUtils.saveTcpClientList(listSp);
                } else if (type == 2) {
                    List<MyDebugBean> listSp = AppSpUtils.getUdpServerList();
                    String type = getString(R.string.udp_server);
                    if (listSp == null) {
                        listSp = new ArrayList<>();
                    }
                    listSp.add(new MyDebugBean(name, host, port, type));
                    AppSpUtils.saveUdpServerList(listSp);
                } else if (type == 3) {
                    List<MyDebugBean> listSp = AppSpUtils.getUdpClientList();
                    String type = getString(R.string.udp_client);
                    if (listSp == null) {
                        listSp = new ArrayList<>();
                    }
                    listSp.add(new MyDebugBean(name, host, port, type));
                    AppSpUtils.saveUdpClientList(listSp);
                }
                ToastUtils.showShort(getString(R.string.save_success));
                finish();
                break;
        }
    }

    @Override
    protected void initData() {
        if (isEdit) {
            mDataBinding.tvAddDebugTitle.setText(getString(R.string.edit));
            if (type == 0) {
                listSp = AppSpUtils.getTcpServerList();
            } else if (type == 1) {
                listSp = AppSpUtils.getTcpClientList();
            } else if (type == 2) {
                listSp = AppSpUtils.getUdpServerList();
            } else if (type == 3) {
                listSp = AppSpUtils.getUdpClientList();
            }
            mDataBinding.etName.setText(listSp.get(selPosition).getName());
            mDataBinding.etHost.setText(listSp.get(selPosition).getHost());
            mDataBinding.etPort.setText(listSp.get(selPosition).getPort());
        } else if (type == 0) {
            mDataBinding.tvAddDebugTitle.setText(getString(R.string.add_tcp_server));
        } else if (type == 1) {
            mDataBinding.tvAddDebugTitle.setText(getString(R.string.add_tcp_client));
        } else if (type == 2) {
            mDataBinding.tvAddDebugTitle.setText(getString(R.string.add_udp_server));
        } else if (type == 3) {
            mDataBinding.tvAddDebugTitle.setText(getString(R.string.add_udp_client));
        }
    }
}